//
//  CYLoginVController.m
//  CreditAddressBook
//
//  Created by Lee on 15/4/23.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYLoginVController.h"
#import "MBProgressHUD+MJ.h"
#import "CYEscape.h"
#import "CYLoginHttpTool.h"
#import "CYUser.h"
#import "MJExtension.h"
#import "CYJumpsTool.h"
#import "MobClick.h"
#import "CYABTool.h"
#import "CYMeTool.h"
#import "CYJsonInfo.h"
#import "CYDES3Util.h"

@interface CYLoginVController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneLbel;
@property (weak, nonatomic) IBOutlet UITextField *passwordLabel;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@end

@implementation CYLoginVController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置取消按钮
    [self setupCancelBtn];
}

/**
 *  设置取消按钮
 */
- (void)setupCancelBtn {
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    btn.tintColor = CYNaviTitleColor;
    self.navigationItem.leftBarButtonItem = btn;
    
    self.loginBtn.layer.cornerRadius = 4;
    self.loginBtn.layer.masksToBounds = YES;
}

/**
 *  取消按钮方法
 */
- (void)cancel {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)logInClick:(id)sender {
    if (self.phoneLbel.text.length && self.passwordLabel.text.length) {
        [MBProgressHUD showMessage:@"登录中..." toView:self.view];
        NSString *phone = self.phoneLbel.text;
        NSString *password = self.passwordLabel.text;
        [CYLoginVController loginWithPhone:phone password:password view:self.view complete:^{
            [self cancel];
        }];
    } else {
        [[[UIAlertView alloc] initWithTitle:@"用户名或登录密码不能为空！" message:nil delegate:nil cancelButtonTitle:CYToolTips otherButtonTitles:nil] show];
    }
    [self.view.window endEditing:YES];
}

+ (void)loginWithPhone:(NSString *)userName password:(NSString *)password view:(UIView *)view complete:(void(^)())complete {
    @try {
        NSString *pw = [CYEscape encrypt:password];
//        [MBProgressHUD hideHUDForView:view];
        [CYLoginHttpTool loginWithUserName:userName andPassword:pw success:^(id json) {
            if (json) {
                CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
                if (jsonInfo.isSuccess) {
                    CYUser *user = [CYUser objectWithKeyValues:jsonInfo.data];
                    [CYLoginHttpTool saveUserLoginInfo:user];
                    //登录后进行云备份
                    [MBProgressHUD hideHUDForView:view];
                    [MBProgressHUD showSuccess:@"登录成功！"];
                    //跳转到Main
                    [CYJumpsTool jumpToMain:view];
                    //统计登录用户
                    [MobClick event:@"login"];
                    if (complete) {
                        complete();
                    }
                } else {
                    [MBProgressHUD hideHUDForView:view];
                    [MBProgressHUD showError:jsonInfo.message];
                }
            }else {
                [MBProgressHUD hideHUDForView:view];
                [MBProgressHUD showError:@"用户名或密码错误！"];
            }
        } failure:^(NSError *error) {
            CYLog(@"登录失败：%@", error);
            [MBProgressHUD hideHUDForView:view];
            [MBProgressHUD showError:@"服务器链接失败！"];
        }];
    }
    @catch (NSException *exception) {
        CYLog(@"登录失败：%@", exception);
        [MBProgressHUD hideHUDForView:view];
        [MBProgressHUD showError:@"服务器链接失败！"];
    }
}

- (void)dealloc {
    CYLog(@"login销毁");
}

#pragma mark - 键盘监听
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

/**
 *  屏幕点击时触发
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CYLog(@"点击屏幕");
    [self.view.window endEditing:YES];
}

@end
